package com.piiworks.google;

import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Spring java configuration for GPX to KML converter
 * 
 * @author gerliczky
 * 
 */
@Configuration
public class GpxConfig
{

	/**
	 * Configures the GPX2KML service
	 * 
	 * @return Gpx2kmlService
	 */
	@Bean
	public Gpx2kmlService gpx2kmlService()
	{
		return new Gpx2kmlService();
	}

	/**
	 * Configures the Log4j file appender
	 * 
	 * @return FileAppender
	 * @throws IOException if the target file cannot be created
	 */
	@Bean
	public FileAppender fileAppender() throws IOException
	{
		PatternLayout patternLayout = new PatternLayout();
		patternLayout.setConversionPattern("%d %-5p %c{1}:%L - %m%n");
		RollingFileAppender fileAppender = new RollingFileAppender(patternLayout, "D:\\Logs\\gpx2kml.log");
		fileAppender.setName("AA");
		fileAppender.setEncoding("UTF-8");
		fileAppender.setThreshold(Level.ALL);
		fileAppender.setMaxFileSize("2MB");
		fileAppender.setMaxBackupIndex(1);
		return fileAppender;
	}

	/**
	 * register the Log4j logger
	 * 
	 * @return Logger
	 * @throws IOException if the target file cannot be created in case of FileAppender
	 */
	@Bean
	public Logger registerSpringLogger() throws IOException
	{
		Logger logger = Logger.getRootLogger();
		logger.addAppender(fileAppender());
		return logger;
	}

}
